package model.client;

import com.alibaba.fastjson.JSONObject;
import javafx.application.Platform;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import view.OnLineView;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ClientListener implements Runnable {
    private Client client;
    private Socket socket;
    private OnLineView onLineView;

    private JSONObject object;

    public ClientListener(Socket socket, Client client, OnLineView onLineView) {
        this.socket = socket;
        this.client = client;
        this.onLineView = onLineView;
    }

    public JSONObject getObject() {
        return object;
    }

    @Override
    public void run() {
        try {
            ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
            while (true) {
                object = (JSONObject) in.readObject();
                String friend_id = object.getString("from");
                String type = object.getString("type");
                if (type.equals("2")) {
                    String sql = "select nickname, portrait from user where user_id = ?";
                    ResultSet resultSet = onLineView.getDatabase().execResult(sql, friend_id);
                    String name = null;
                    String portrait = null;
                    if (resultSet.next()) {
                        name = resultSet.getString("nickname");
                        portrait = resultSet.getString("portrait");
                    }
                    System.out.println(name + "----" + portrait);
                    String finalName = name;
                    String finalName1 = name;
                    String finalPortrait = portrait;
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                onLineView.receiveMsgFromFriend(finalName, object.getString("msg"), finalName1, finalPortrait);
                                ListView fList = (ListView) onLineView.findViewById("fList");
                                for (int i=0; i<fList.getItems().size(); i++) {
                                    AnchorPane pane = (AnchorPane)fList.getItems().get(i);
                                    String name = ((Label) pane.lookup("#name")).getText();
                                    if (name.equals(finalName)) {
                                        ((Label) pane.lookup("#msg")).setText(object.getString("msg"));
                                    }
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    resultSet.close();
                }
                if (type.equals("3")) {
                    String sql = "select nickname, portrait from user where user_id = ?";
                    ResultSet resultSet = onLineView.getDatabase().execResult(sql, friend_id);
                    String name = null;
                    String portrait = null;
//                    onLineView.receiveMsgFromGroup();
                    if (resultSet.next()) {
                        name = resultSet.getString("nickname");
                        portrait = resultSet.getString("portrait");
                    }

                    System.out.println(name + "----" + portrait);
                    String finalName = name;
                    String finalPortrait = portrait;
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                onLineView.receiveMsgFromGroup(finalName, object.getString("msg"), object.getString("gn"), finalPortrait);
                                ListView gList = (ListView) onLineView.findViewById("gList");
                                for (int i=0; i<gList.getItems().size(); i++) {
                                    AnchorPane pane = (AnchorPane)gList.getItems().get(i);
                                    String name = ((Label) pane.lookup("#name")).getText();
                                    if (name.equals(object.getString("gn"))) {
                                        ((Label) pane.lookup("#msg")).setText(finalName + ": " + object.getString("msg"));
                                    }
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            try {
                socket.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            System.out.println("与服务器失去连接");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
